<?php
ob_start();
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 

$query="select ID from Trail where name like '$_POST[TrailName]';";
$resultID=mysql_query($query);
if (!$resultID) {
  echo "Error selecting ID from Trail: " . mysql_error();
  die("");
}
$trailID=mysql_result($resultID,0, "ID");

// deletes all reviews associated w/ the trail
$query=" DELETE FROM Review where trailID=$trailID; ";
if (!mysql_query($query)) {
  echo "Error deleting reviews: " . mysql_error();
}

// deletes all saved trails associated w/ the trail
$query=" DELETE FROM Saved where trailID=$trailID; ";
if (!mysql_query($query)) {
  echo "Error deleting saved trails: " . mysql_error();
}

// deletes the trail
$query=" DELETE FROM Trail where ID=$trailID; ";
if (!mysql_query($query)) {
  echo "Error deleting trail: " . mysql_error();
}

mysql_close();
header("Location: index.php");
ob_flush();
?> 
